<template>
    <!-- 安全制度管理-新增、编辑上报 -->
    <base-breadcrumb :breadcrumb="!isAudit">
        <div class="page-body">
            <a-card :bordered="false">
                <title-name title="基本信息" />
                <a-form-model ref="form" :model="formInfo" :rules="rules" :hideRequiredMark="true">
                    <a-row :gutter="24">
                        <!-- <a-col :md="8" :sm="8">
              <a-form-model-item
                label="制度分类"
                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                prop="sysTypeId"
              >
                <a-select placeholder="请选择制度分类" v-model="formInfo.sysTypeId" :disabled="isDisabled">
                  <a-select-option v-for="item in classifyList" :key="item.id" :value="item.id">{{
                    item.name
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-col> -->
                        <a-col :md="16" :sm="16">
                            <a-form-model-item
                                    label="所属单位"
                                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                                    prop="projectId"
                            >
                                <org-tree-select
                                        :is-query-projec-detail="false"
                                        is-auto-select-first
                                        is-need-auth
                                        check-strictly
                                        :disabled="isDisabled"
                                        v-model="formInfo.projectId"
                                        @select="selectOrg"
                                />
                            </a-form-model-item>
                        </a-col>
<!--                        <a-col :md="8" :sm="8">-->
<!--                            <a-form-model-item-->
<!--                                label="所属分公司"-->
<!--                                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"-->
<!--                                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"-->
<!--                                prop="branchComId"-->
<!--                            >-->
<!--                                <SelectSubCompany :disabled="isDisabled" v-model="formInfo.branchComId" />-->
<!--                            </a-form-model-item>-->
<!--                        </a-col>-->
<!--                        <a-col :md="8" :sm="8">-->
<!--                            <a-form-model-item-->
<!--                                label="所属项目"-->
<!--                                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"-->
<!--                                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"-->
<!--                                prop="projectId"-->
<!--                            >-->
<!--                                <SelectProject-->
<!--                                    :disabled="isDisabled"-->
<!--                                    v-model="formInfo.projectId"-->
<!--                                    keyValue="branchComId"-->
<!--                                    :form="formInfo"-->
<!--                                    :clear-list="true"-->
<!--                                />-->
<!--                            </a-form-model-item>-->
<!--                        </a-col>-->

                        <a-col :md="8" :sm="8">
                            <a-form-model-item
                                label="制度名称"
                                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                                prop="name"
                            >
                                <BaseInput
                                    :disabled="isDisabled"
                                    placeholder="请输入制度名称"
                                    v-model="formInfo.name"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="8" :sm="8">
                            <a-form-model-item
                                :disabled="isDisabled"
                                label="编制人"
                                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                                prop="redactString"
                            >
                                <SelectPersonByProject
                                    :disabled="isDisabled"
                                    :projectId="formInfo.projectId"
                                    v-model="formInfo.redactString"
                                />
                            </a-form-model-item>
                        </a-col>
                        <a-col :md="16" :sm="16">
                            <a-form-model-item
                                label="附件上传"
                                :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                                :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }"
                            >
                                <upload-list
                                    :disabled="isDisabled"
                                    :businessCode="'SHE05'"
                                    :uploadArrys.sync="formInfo.fileInfoVOList"
                                    @fileArrysData="handleFileUploaded"
                                    :isClose="!isDisabled"
                                    multiple
                                ></upload-list>
                            </a-form-model-item>
                        </a-col>
                    </a-row>
                </a-form-model>
            </a-card>
            <div class="tool-btns page-btn-right-top" v-if="pageType !== 'look'">
                <a-button
                    v-if="formInfo.status === 'DRAFT' && pageType !== 'add'"
                    type="danger"
                    :loading="loading.DELETE"
                    @click="handleSave('DELETE')"
                    >删除草稿</a-button
                >
                <a-button
                    v-if="formInfo.status === 'DRAFT'"
                    type="primary"
                    :loading="loading.DRAFT"
                    @click="handleSave('DRAFT')"
                    >保存草稿</a-button
                >
                <a-button type="primary" :loading="loading.SUBMITTED" @click="handleSave('SUBMITTED')">提交</a-button>
                <a-button type="primary" @click="handleClose">关闭</a-button>
            </div>
            <div class="tool-btns page-btn-right-top" v-else>
                <a-button type="primary" @click="handleClose"  v-if="checkVisibleInDetail">关闭</a-button>
            </div>
        </div>
       <selectApprover :businessId="businessId"  :processConfig="setProcessConfig"></selectApprover>
    </base-breadcrumb>
</template>

<script>
import SelectPersonByProject from '@/components/Select/SelectPersonByProject'
import SelectSubCompany from '@/components/Select/SelectSubCompany.vue'
import SelectProject from '@/components/Select/SelectProject.vue'
import UploadList from '@/components/UploadList'
import {
    addSystemManage,
    deleteSystemManage,
    getSystemManageDetail,
    updateSystemManage,
    getSystemClassifyList,
} from '@/api/safetySupervision/safetySystem.js'
import {  checkVisibleInDetail} from '@/utils/util'
export default {
    name:'a'+Date.now(),    components: {
        UploadList,
        SelectSubCompany,
        SelectProject,
        SelectPersonByProject,
    },
    data() {
        return {
            checkVisibleInDetail:checkVisibleInDetail(this),
            classifyList: [],
            loading: {
                DELETE: false,
                DRAFT: false,
                SUBMITTED: false,
            },
            formInfo: {
                redactString: '',
                sysTypeId: '',
                branchComId: '',
                projectId: '',
                name: '',
                status: 'DRAFT', // DRAFT, SUBMITTED, PAST, REJECTED, DELETE
                fileInfoVOList: [],
            },
            rules: {
                sysTypeId: [{ required: true, message: '请选择制度类型', trigger: 'change' }],
                branchComId: [{ required: true, message: '请选择所属分公司', trigger: 'change' }],
                projectId: [{ required: true, message: '请选择所属项目', trigger: 'change' }],
                name: [{ required: true, message: '请输入制度名称', trigger: 'change' }],
                redactString: [{ required: true, message: '请选择编制人', trigger: 'change' }],
            },
            //获取流程配置值
            processConfig: null,
            //点击提交发送流程配置值
            setProcessConfig: null,
            businessId: null
        }
    },
    props: {
        isAudit: {
            // 是否作为审批详情
            type: Boolean,
            default: false,
        },
    },
    watch: {
        isAudit: {
            immediate: true,
            handler(val) {
                if (val) {
                    this.$nextTick(() => {
                        this.pageType = 'look'
                    })
                }
            },
        },
    },
    computed: {
        pageType() {
            return this.$route.query.pageType || 'look'
        },
        isDisabled() {
            return this.pageType === 'look'
        },
    },
   async created() {
        this.getDetail()
        this.getSystemClassifyList()
        //获取通过获取流程配置值
        this.processConfig = await this.$getConfigFn('safe_security_system')
    },
    methods: {
      selectOrg(val) {
        if(val.length > 1) {
          this.formInfo.branchComId = val[val.length - 2].orgId
        }
      },
        handleFileUploaded(list) {
            this.formInfo.fileInfoVOList = [...list]
        },
        async getSystemClassifyList() {
            const [err, res] = await getSystemClassifyList()
            if (!err) {
                this.classifyList = (res.data || []).filter((item) => !!item.enabled)
            }
        },
        validateForm() {
            let flag = false
            this.$refs.form.validate((valid) => {
                flag = valid
            })
            return flag
        },
        async getDetail() {
            const { id } = this.$route.query
            if (!id) return
            const params = {
                id,
            }
            const [err, res] = await getSystemManageDetail(params)
            if (!err) {
                this.formInfo = {
                    ...this.formInfo,
                    ...res.data,
                    redactString:
                        res.data.redactString && res.data.redactString.length > 0 ? res.data.redactString[0] : '',
                }
                this.$nextTick(() => {
                    this.formInfo.projectId = res.data.projectId
                })
            }
        },
        // 处理增、删、改操作
        async handleSave(status) {
            this.setProcessConfig = null;
            if (status !== 'DELETE' && !this.validateForm()) return
            if (this.loading[status]) return
            this.loading[status] = true
            const params = {
                ...this.formInfo,
                redactString: [this.formInfo.redactString],
                status,
                id: this.$route.query.id || void 0,
            }
            const { pageType } = this.$route.query
            const fn = pageType === 'edit' ? updateSystemManage : addSystemManage
            const map = {
                DELETE: deleteSystemManage,
                DRAFT: fn,
                SUBMITTED: fn,
            }
            if(status =='SUBMITTED') {
                this.submitApprovalConfirm(this.processConfig, async (flag)=>{
                  if(!flag) {
                    this.loading[status] = false
                    return
                  }
                  const [err, res] = await map[status](params)
                  if (!err) {
                    this.$message.success('成功')
                    if(status =='SUBMITTED') {
                      //提交时，返回id
                      this.businessId = res.data.id;
                      this.setProcessConfig = this.processConfig;
                      return
                    }else{
                      this.handleClose()
                    }
                  }
                  this.loading[status] = false
                })
            } else {
            const [err, res] = await map[status](params)
            if (!err) {
                this.$message.success('成功')
                if(status =='SUBMITTED') {
                //提交时，返回id
                this.businessId = res.data.id;
                this.setProcessConfig = this.processConfig;
                return
            }else{
            this.handleClose()
          }
            }
            this.loading[status] = false
            }
        },
        handleClose() {
             if (this.isAudit) {
                window.close()
            } else {
                this.$multiTab.close(this.$route.fullpath);
            }
        },
    },
}
</script>

<style lang="less" scoped>
@deep: ~'>>>';
.tool-btns {
    display: flex;
    justify-content: flex-end;
    @{deep} .ant-btn + .ant-btn {
        margin-left: 12px;
    }
}
</style>
